/*
 * Copyright (C) 2006-2012 TongYan Corporation
 * All rights reserved.
 *
 * @brief info:
 *
 * @author: 王哲成 - wangzhecheng@yeah.net
 * @date: 2021.05.25
 * @last modified: 2021-05-25 10:58
 *
 */

#include "../include/Param.hpp"

void ParamSet::append(const std::string &name, const double &lbound,
                      const double &ubound, const size_t &nstep) {
  assert(ubound > lbound);
  assert(nstep > 0);

  size_t n = m_dUBound.size();
  m_dUBound.conservativeResize(n + 1);
  m_dLBound.conservativeResize(n + 1);
  m_dDelta.conservativeResize(n + 1);
  m_vecstrName.push_back(name);
  m_dUBound[n + 1] = ubound;
  m_dLBound[n + 1] = lbound;
  m_dDelta[n + 1] = (ubound - lbound) / static_cast<double>(nstep);
  return;
}
size_t ParamSet::size() const { return m_dDelta.size(); }

// 按索引号搜索第n维的增量(求差分用)
double ParamSet::dx(const size_t &n) const { return m_dDelta[n]; }
// 按名称搜索第n维的增量(求差分用)
double ParamSet::dx(const std::string &name) const {
  for (size_t i = 0; i != m_vecstrName.size(); i++)
    if (m_vecstrName[i] == name)
      return m_dDelta[i];
  exit(1);
  return 0.0;
}

// 返回增量向量(求差分用)
Vector ParamSet::dx() const { return m_dDelta; }

// 按索引号搜索第n维的定义域上限(生成随机点用)
double ParamSet::uBound(const size_t &n) const { return m_dUBound[n]; }
// 按名称搜索第n维的定义域上限(生成随机点用)
double ParamSet::uBound(const std::string &name) const {
  for (size_t i = 0; i != m_vecstrName.size(); i++)
    if (m_vecstrName[i] == name)
      return m_dUBound[i];
  exit(1);
  return 0.0;
}

// 按索引号搜索第n维的定义域下限(生成随机点用)
double ParamSet::lBound(const size_t &n) const { return m_dLBound[n]; }
// 按名称搜索第n维的定义域下限(生成随机点用)
double ParamSet::lBound(const std::string &name) const {
  for (size_t i = 0; i != m_vecstrName.size(); i++)
    if (m_vecstrName[i] == name)
      return m_dLBound[i];
  exit(1);
  return 0.0;
}
